package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.PublicationsRead;
import com.yg.entity.singleTemplates.Publications;
import com.yg.service.PublicationsService;
import com.yg.utils.CloseStream;
import com.yg.utils.DownLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 对应sheet14，此表是本科生发表文章表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/publications")
@CrossOrigin
public class PublicationsController {
    @Autowired
    private PublicationsService publicationsService;

    /**
     * 读取excel写入数据库
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(MultipartFile file) {
        //获取fileInputStream
        System.out.println(file);
        FileInputStream fileInputStream = null;
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = publicationsService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }

    /**
     * 导出excel
     *
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletResponse response, @RequestParam(required = false) String title, String startAndEnd, String physicsProperties) throws UnsupportedEncodingException {
        PublicationsRead publicationsRead = new PublicationsRead();
        publicationsRead.setTitle(title);
        publicationsRead.setStartAndEnd(startAndEnd);
        publicationsRead.setPhysicsProperties(physicsProperties);
        R r = publicationsService.writeExcel(response, publicationsRead);
        return r;
    }

    /**
     * 条件查询
     * 查询所有
     *
     * @param
     * @return
     */
    @PostMapping("/selectPublicationsByCondition")
    public R publicationConditionalQuery(String current, String limit, @RequestBody(required = false) PublicationsRead publicationsRead) {
        R r = null;
        if (publicationsRead == null ) {
            r = publicationsService.selectAllPublications(current, limit);
        } else {
            r = publicationsService.conditionalQuery(publicationsRead, current, limit);
        }
        return r;
    }

    /**
     * 查询所有
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/getPublications")
    public R getPublicatons(String current,String limit){
        R r = publicationsService.selectAllPublications(current, limit);
        return r;
    }



    /**
     * 根据id删除教师信息
     *
     * @param
     * @return
     */
    @DeleteMapping("/deletePublications")
    public R deletePublicationsById(@RequestBody Publications publications) {
        R r = publicationsService.deletePublicationById(publications);
        return r;
    }

    /**
     * 更新
     *
     * @param publications
     * @return
     */
    @PutMapping("/putPublications")
    public R updatePublications(@RequestBody Publications publications) {
        R r = publicationsService.updatePublications(publications);
        return r;
    }

    /**
     * 新增
     *
     * @param publications
     * @return
     */
    @PostMapping("/postPublications")
    public R insertPublications(@RequestBody Publications publications) {
        R r = publicationsService.insertPublications(publications);
        return r;
    }
}

